package service
{
	import Model.Post;
	import Model.Topic;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.*;
	
	import mx.charts.chartClasses.StackedSeries;

	public class MessageHandler
	{
//		<fx:Metadata>
//			[Event(name="recievedNewPost", type="model.event.RecievedNewPost")]
//			[Event(name="recievedEditedPostsText", type="model.event.RecievedEditedPostsText")]
//			[Event(name="recievedEditedPostsTopic", type="model.event.RecievedEditedPostsTopic")]
//			[Event(name="recievedDeletedPost", type="model.event.RecievedDeletedPost")]
//			[Event(name="reloadTopics", type="model.event.ReloadTopics")]
//			[Event(name="recievedWritingNotification", type="model.event.RecievedWritingNotification")]
//		</fx:Metadata>
		
		public static var _instance:MessageHandler;
		
		public function MessageHandler()
		{
			
		}
		
		public function initialize():void
		{
			var url:String = "http://classalc.local/chatajax/request.php";
			var arguments:URLVariables = new URLVariables();
			httpRequest(url, arguments);
			
			
			//HTTP --> Visitor and Path
			//HTTM <-- users
			//HTTP <-- topics
			//HTTP <-- load(null, 24 hours ago)
			//HTTP <-- which posts are unseen
						//HTTP <-- which posts have been moved (and we haven't been notified, yet)
			//HTTP <-- RTMP's Remote Shared Object's name
			
			//RTMP --> connect to the Remote Shared Object
		}
		
//		public function sendPost(post:Post):void
//		{
//			httpSend("", post.text, post.author, post.date, post.topic);
//			rtmpSend("", post.text, post.author, post.date, post.topic);
//		}
//		
//		public function editPostText(post:Post):void
//		{
//			httpSend("", post.id, post.text, post.date);
//			rtmpSend("", post.id, post.text, post.date);
//		}
//		
//		public function editPostTopic(post:Post):void
//		{
//			httpSend("", post.id, post.topic, new Date());
//			rtmpSend("", post.id, post.topic, new Date());
//		}
//		
//		public function deletePost(post:Post):void
//		{
//			httpSend("", post.id, new Date());
//			rtmpSend("", post.id, new Date());
//		}
//		
//		public function load(topic:Topic = null, startDate:Date = null):Array
//		{
//			if(topic == null)
//				return httpRequest("loadAllTopics", startDate);
//			else
//				return httpRequest("loadTopic", topic, startDate);
//		}
//		
//		public function addTopic(topic:Topic):void
//		{
//			httpSend("", topic.name, topic.author, topic.date);
//			rtmpSend("reloadTopics");
//		}
//		
//		public function editTopic(topic:Topic):void
//		{
//			httpSend("", topic.id, topic.name, topic.author, topic.date);
//			rtmpSend("reloadTopics");
//		}
//		
//		public function deleteTopic(topic:Topic):void
//		{
//			httpSend("", topic.id);
//			rtmpSend("reloadTopics");
//		}
//		
//		public function changeWritingStatus(newStatus:string, topic:Topic):void
//		{
//			if(newStatus == "not writing" || newStatus == "all deleted")
//				rtmpSend("", newStatus, topic.id);
//			else if(newStatus == "writing")
//			{
//				//send this every 1 second
//				rtmpSend("", newStatus, topic.id);
//			}
//		}
//		
//		
//		private function httpSend(reciever:String, arguments:unknownYet):GeneralType
//		{
//			
//		}
		
		private function httpRequest(url:String, arguments:URLVariables):void
		{
			var request:URLRequest = new URLRequest(url);
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			request.data = arguments;
			request.method = URLRequestMethod.POST;
			loader.addEventListener(Event.COMPLETE, handleHttpComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			loader.load(request);
			
		}
		
		private function handleHttpComplete(event:Event):void 
		{
			var loader:URLLoader = URLLoader(event.target);
			trace("data: " + loader.data);
		}
		private function onIOError(event:IOErrorEvent):void 
		{
			trace("Error loading URL." + event.toString());
		}
		
//		private function rtmpSend(reciever:String, arguments:unknownYet):GeneralType
//		{
//			
//		}
	}
}